package com.example.fx.detail

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.base.entity.AddCarEntityResult
import com.example.base.entity.CommentEntityResult
import com.example.base.entity.DetailEntityResult
import com.example.base.mvvm.BaseViewModel
import com.google.gson.Gson
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

class DetailViewModel:BaseViewModel() {
    val detailSuccess = MutableLiveData<DetailEntityResult>()
    val detailFail = MutableLiveData<String>()
    fun detail(id:Int,type:String){
        viewModelScope.launch {
            try {
                detailSuccess.value = apiService.detail(id,type)
            }catch (e:Exception){
                detailFail.value = e.message
            }
        }
    }
    //添加购物车
    val addCarSuccess = MutableLiveData<AddCarEntityResult>()
    val addCarFail = MutableLiveData<String>()
    //加入购物车数量  属性id   商品id
    fun addCar(cartNum:Int,productAttrUnique:String,productId:Int){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map.put("cartNum",cartNum)
                map.put("productAttrUnique",productAttrUnique)
                map.put("productId",productId)
                val json = Gson().toJson(map)
                val body = RequestBody.create("application/json;charset=utf-8".toMediaTypeOrNull(),json)
                addCarSuccess.value = apiService.addCar(body)
            }catch (e:Exception){
                addCarFail.value = e.message
            }
        }
    }
    val getSuccess = MutableLiveData<CommentEntityResult>()
    val getFail = MutableLiveData<String>()
    fun commentGet(){
        viewModelScope.launch {
            try {
                getSuccess.value = apiService.commentGet()
            }catch (e:Exception){
                getFail.value = e.message
            }
        }
    }
    val commentSuccess = MutableLiveData<CommentEntityResult>()
    val commentFail = MutableLiveData<String>()
    fun comment(msg:String,videoId:Int){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map.put("msg",msg)
                map.put("videoId",videoId)
                val json = Gson().toJson(map)
                val body = RequestBody.create("application/json;charset=utf-8".toMediaTypeOrNull(),json)
                commentSuccess.value = apiService.comment(body, "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhdXRoMCIsImV4cCI6MTczOTg0ODg1MiwidXNlcm5hbWUiOiIxIn0.beJAGGAdhptvJAMCNhlh2mp9m13XGkTN-k_RKCSrcAk")
            }catch (e:Exception){
                commentFail.value = e.message
            }
        }
    }
    //获取二级评论
    val childGetSuccess = MutableLiveData<CommentEntityResult>()
    val childGetFail = MutableLiveData<String>()
    fun commentChildGet(rootId:Int){
        viewModelScope.launch {
            try {
                childGetSuccess.value = apiService.commentChildGet(rootId)
            }catch (e:Exception){
                childGetFail.value = e.message
            }
        }
    }
}